/*
 * PeakReference.cpp
 *
 *  Created on: Jan 6, 2011
 *      Author: blyth
 */

#include "PeakReference.h"

PeakReference::PeakReference(double* ref, int size) {
	this->size = size+2;
	this->ref = new double[size+2];
	for(int i = 1;i < size+2;i++)
		(this->ref)[i] = ref[i-1];
	(this->ref)[0] = ref[0]+(ref[0]-ref[1]);
	(this->ref)[size+1] = ref[size-1]+(ref[size-1]-ref[size-2]);
}

PeakReference::~PeakReference() {
	delete ref;
}

void PeakReference::feedNewDescreteReference(double* ref, int size){
	delete this->ref;
	this->size = size+2;
	this->ref = new double[size+2];
	for(int i = 1;i < size+2;i++)
		(this->ref)[i] = ref[i-1];
	(this->ref)[0] = ref[0]+(ref[0]-ref[1]);
	(this->ref)[size+1] = ref[size-1]+(ref[size-1]-ref[size-2]);
}

double PeakReference::cubicInterpolationAt(double xPos){
	int x1 = (int)xPos+1;
	xPos -= (int)xPos;

	double xPos2 = xPos*xPos;
	double a0 = .5*ref[x1+2]-1.5*ref[x1+1]+1.5*ref[x1]-0.5*ref[x1-1];
	double a1 = -0.5*ref[x1+2]+2*ref[x1+1]-2.5*ref[x1]+ref[x1-1];
	double a2 = 0.5*ref[x1+1]-0.5*ref[x1-1];
	double a3 = ref[x1];

	return(a0*xPos*xPos2+a1*xPos2+a2*xPos+a3);
}
